<?php

/**
*	修改个人详情
*
*/


//包含公共文件
include '../../common/common.php';

//接收post的传值
// var_dump($_POST);
$uid	    = trim($_POST['uid']);
$newPwd  = trim($_POST['newpwd']);
$isClean    = trim($_POST['isclean']);
$isLock     = trim($_POST['islock']);
$email      = trim($_POST['email']);
$regIp      = trim($_POST['regip']);
$regTime  = trim($_POST['regtime']);
$lastTime  = trim($_POST['lasttime']);
//积分,签名
$grade      = trim($_POST['grade']);
$autograph= trim($_POST['autograph']);
//用户栏目
$realName = trim($_POST['realname']);
$sex 	     = trim($_POST['sex']);
$year        = trim($_POST['birthyear']);
$month     = trim($_POST['birthmonth']);
$day         = trim($_POST['birthday']);
$place       = trim($_POST['place']);

$birthday   = strtotime("$year-$month-$day");
// var_dump($birthday);
//打印
// var_dump($oldPwd);
// var_dump($newPwd);	 
// var_dump($rePwd);	 	 
//var_dump($email);   
// var_dump($question);         
// var_dump($answer);
// var_dump($regIp);

//查询用户当前的密码

$resMsg   =  mySelect($link , '*' , DB_TABLE_USER , "where uid = $uid");
$resPwd   = $resMsg[0]['password'];
$resEmail = $resMsg[0]['email'];
$resRegIp = $resMsg[0]['regip'];
$resGrade = $resMsg[0]['grade'];
$resRegTime   = $resMsg[0]['regtime'];
$resLastTime   = $resMsg[0]['lasttime'];
$resAutograph = $resMsg[0]['autograph'];
$resAllowLogin = $resMsg[0]['allowlogin'];

// var_dump($resMsg);

//判断是否修改密码
if ($newPwd) {
	
	//判断新密码长度
	if (strlen($newPwd) < 3 || strlen($newPwd) > 12) {

		exit('密码长度只能在3~12位字符之间<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
	}
	//判断新密码是否为纯数字
	if (is_numeric($newPwd)) {

		exit('新密码不能为纯数字<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
	}
	//判断两次新密码是否一致
	if ($newPwd !== $resPwd) {

		$data['password'] = md5($newPwd); 
	} 
}

//判断是否清除安全提问
if ($isClean == 1) {
	$data['question'] = '';
	$data['answer'] = '';
}
//判断是否锁定登录状态
if ($isLock !== $resAllowLogin) {

	$data['allowlogin'] = $isLock;
} 

//个人信息
//判断是否输入邮箱
if ($email) {

	//判断邮箱格式是否正确
	$ePattern = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
	if (!preg_match($ePattern,$email)) {

		exit('邮箱格式错误<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
	}

	//判断是否与原邮箱相等
	if ($email != $resEmail) {

		$data['email'] = "$email";
	}

} else {

	exit('邮箱不得为空<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}

//判断注册时间
if ($regIp) {

	$regIp2 = ip2long($regIp);
	// var_dump($regIp2);

	//判断ip格式
	if (!$regIp2) {

		exit('IP格式错误,请重新输入<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
	}
	//判断IP格式是否更改
	if ($regIp2 != $resRegIp) {

		$data['regip'] = $regIp2;
	} 

} else {

	exit('注册IP不得为空<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}

//判断注册时间
if (!$regTime) {

	exit('注册时间不得为空<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}

//转化为时间戳
$regTime2 = strtotime("$regTime");
// var_dump($regTime2);

//判断时间戳格式
if ($regTime2) {
	
	//判断是否超出当前时间
	if ($regTime2 > time()) {

		exit('注册时间不得超出当前时间<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
	}
	if ($resRegTime != $regTime2) {

		$data['regtime'] = $regTime2;
	} /*else {
		$data['regtime'] = $resRegTime;
	}
	*/

} else {

	exit('时间格式错误<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}

//判断上次登陆时间
if  ($lastTime != $resLastTime) {

	//判断上次登陆时间是否为空
	if (!$lastTime) {

		exit('注册时间不得为空<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
	}

	//转化为时间戳
	$lastTime = strtotime("$lastTime");
	// var_dump($lastTime);

	//判断时间戳格式
	if ($lastTime) {
		
		//判断是否超时
		if ($resRegTime != $regTime2) {
			
			//判断是否超时
			if ($lastTime < $regTime2) {

				exit('上次登陆时间不得小于注册时间<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
			}
		
		} else {

			//判断是否超时
			if ($lastTime < $resRegTime) {

				exit('上次登陆时间不得小于注册时间<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
			}

		}
		//判断时间格式
		$data['lasttime'] = $lastTime;

	} else {

		exit('时间格式错误<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
	}

}

//积分奖惩
//计算积分
$grade2   = $resGrade + ($grade);

//判断是否超出积分范围
if ($grade2 < 0) {
	
	exit('当前用户积分为<span style="color:red;"> ' . $resGrade . ' </span>你所减的积分,已超出用户积分<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}
//获取用户等级
$levName = myLevel($grade2)[1];
// var_dump($grade2);
// var_dump($levName);

$data['grade'] = $grade2;
$data['levname'] = "$levName";

//论坛选项
//判断签名
if ($autograph != $resAutograph) {

	$data['autograph'] = "$autograph";
}


//用户选项
//判断真实姓名
if ($realName) {

	$data['realname'] = "$realName";
}

//判断生日格式
if (!(($year &&  $month  &&  $day)  ||  (!$year    &&   !$month   &&   !$day)))  {
	exit('输入完整的生日,年月日<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}

if ($birthday) {
	$data['birthday'] = $birthday;
}

//判断籍贯地
if ($place) {
	$data['place'] = "$place";
}

$data['sex']  = $sex;
// var_dump($data);

//修改数据库
$resUser = myUpdate($link , DB_TABLE_USER , $data , "uid = $uid");
// var_dump($resUser);

//处理结果集
if ($resUser && mysqli_affected_rows($link)) {

	echo '修改成功';
} else {

	echo '修改失败';
}

//跳转到上一页
header('refresh:2;url=' . $_SERVER['HTTP_REFERER']);